#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <thread>
#include <mutex>
#include <chrono>
using namespace std;

//int main()
//{
//	size_t n1 = 10000;
//	size_t n2 = 10000;
//
//	mutex mtx;
//	
//	//size_t x = 0;
//	atomic<size_t> x;
//
//	thread t1([&]() {
//		for (size_t i = 0; i < n1; i++)
//		{
//			x++;
//		}
//		});
//
//	thread t2([&]() {
//		for (size_t i = 0; i < n2; i++)
//		{
//			x++;
//		}
//		});
//
//	t1.join();
//	t2.join();
//	cout << x << endl;
//	return 0;
//}

#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <thread>
#include <mutex>
using namespace std;

int main()
{
	size_t n1 = 10000;
	size_t n2 = 10000;

	mutex mtx;

	//size_t x = 0;
	atomic<size_t> x;

	thread t1([&]() {
		for (size_t i = 0; i < n1; i++)
		{
			size_t oldval, newval;
			do
			{
				oldval = x;
				newval = oldval + 2;
				
			} while (!atomic_compare_exchange_weak(&x, &oldval, newval));
		}
		});

	thread t2([&]() {
		for (size_t i = 0; i < n2; i++)
		{
			size_t oldval, newval;
			do
			{
				oldval = x;
				newval = oldval + 2;

			} while (!atomic_compare_exchange_weak(&x, &oldval, newval));
		}
		});

	t1.join();
	t2.join();
	cout << x << endl;
	return 0;
}